﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Class1
/// </summary>
public class Graph
{
  public string libaryAccesString { get; set; }

  public Graph()
  {
    UseLibary();
  }

  public string drawGraphOneAttributeWithFiveFigures(string title, string xAxisName, string firstDataSetName,
      string[] firstDataSet, string[] xAxisData, bool columnChart, bool percentChart, bool units)
  {
    string chartType = "LineChart";
    string graphLine = "";
    string format = "#,###";
    string maxValue = FindValues(firstDataSet, true).ToString();
    string minValue = FindValues(firstDataSet, false).ToString();
    if (columnChart) chartType = "ColumnChart";
    if (percentChart) format = "#,###%";
    if (units) format = "#.###";

    graphLine += " <script type=\"text/javascript\">";
    graphLine += " google.load(\"visualization\", \"1\", {packages:[\"corechart\"]});";
    graphLine += " google.setOnLoadCallback(drawChart);";
    graphLine += " function drawChart() {";
    graphLine += " var data = google.visualization.arrayToDataTable([";
    graphLine += " ['" + xAxisName + "', '" + firstDataSetName + "'],";
    graphLine += " ['" + xAxisData[0] + "',  " + firstDataSet[0] + "],";
    graphLine += " ['" + xAxisData[1] + "',  " + firstDataSet[1] + "],";
    graphLine += " ['" + xAxisData[2] + "',  " + firstDataSet[2] + "],";
    graphLine += " ['" + xAxisData[3] + "',  " + firstDataSet[3] + "],";
    graphLine += " ['" + xAxisData[4] + "',  " + firstDataSet[4] + "]]);";
    graphLine += " var options = {";
    graphLine += " title: '" + title + "',";
    graphLine += " titleTextStyle: {color: '#516f91', fontName: 'Open sans', fontSize: 18},";
    graphLine += " vAxis: {minValue:"+minValue+", maxValue:"+maxValue+", textStyle: {color: '#666', fontName: 'Open sans', fontSize: 12}, format: '"+format+"'},";
    graphLine += " hAxis: {textStyle: {color: '#516f91', fontName: 'Open sans', fontSize: 12} },";
    graphLine += " tooltip: {textStyle: {color: '#666', fontName: 'Open sans', fontSize: 12}},";
    graphLine += " backgroundColor: {fill: '#fcfcfc', stroke: '#fcfcfc', strokeWidth: '2'},";
    graphLine += " series: [{color: '#516f91'}],";
    graphLine += " chartArea:{left:60,top:50, width: '80%', height: '55%'}};";
    graphLine += " var formatter = new google.visualization.NumberFormat({pattern: '" + format + "'});";
    graphLine += " formatter.format(data, 1);";
    graphLine += " var chart = new google.visualization."+chartType+"(document.getElementById('"+title+"'));";
    graphLine += " chart.draw(data, options);}";
    graphLine += " </script>";
    graphLine += " <div id=\""+title+"\" style=\"width: 50%; height: 50%;\"></div>";
    return graphLine;
  }

  public string drawGraphTwoAttributeWithFiveFigures(string title, string xAxisName, string firstDataSetName,
   string secondDataSetName, string[] firstDataSet, string[] secondDataSet, string[] xAxisData, bool columnChart,
    bool percentChart)
  {
    string chartType = "LineChart";
    string graphLine = "";
    string format = "#";
    string maxValue = FindValues(firstDataSet, true).ToString();
    string minValue = FindValues(firstDataSet, false).ToString(); 
    if (columnChart) chartType = "ColumnChart";
    if (percentChart) format = "#,###%";

    graphLine += "<script type=\"text/javascript\">";
    graphLine += "google.load(\"visualization\", \"1\", {packages:[\"corechart\"]});";
    graphLine += "google.setOnLoadCallback(drawChart);";
    graphLine += "function drawChart() {";
    graphLine += "var data = google.visualization.arrayToDataTable([";
    graphLine += "['" + xAxisName + "', '" + firstDataSetName + "','" + secondDataSetName + "'],";
    graphLine += "['" + xAxisData[0] + "',  " + firstDataSet[0] + ", " + secondDataSet[0] + "],";
    graphLine += "['" + xAxisData[1] + "',  " + firstDataSet[1] + ", " + secondDataSet[1] + "],";
    graphLine += "['" + xAxisData[2] + "',  " + firstDataSet[2] + ", " + secondDataSet[2] + "],";
    graphLine += "['" + xAxisData[3] + "',  " + firstDataSet[3] + ", " + secondDataSet[3] + "],";
    graphLine += "['" + xAxisData[4] + "',  " + firstDataSet[4] + ", " + secondDataSet[4] + "]]);";
    graphLine += "var options = {";
    graphLine += "title: '" + title + "',";
    graphLine += "titleTextStyle: {color: '#516f91', fontName: 'Open sans', fontSize: 18},";
    graphLine += " vAxis: {minValue:0, maxValue:" + maxValue + ", textStyle: {color: '#666', fontName: 'Open sans', fontSize: 12}, format: '" + format + "'},";
    graphLine += "hAxis: {textStyle: {color: '#516f91', fontName: 'Open sans', fontSize: 12} },";
    graphLine += "tooltip: {textStyle: {color: '#666', fontName: 'Open sans', fontSize: 12}},";
    graphLine += "backgroundColor: {fill: '#fcfcfc', stroke: '#fcfcfc', strokeWidth: '2'},";
    graphLine += "colors: ['#516f91', '#4c937d'],";
    graphLine += " chartArea:{left:60,top:50, width: '80%', height: '55%'}};";
    graphLine += "var formatter = new google.visualization.NumberFormat({pattern: '" + format + "'});";
    graphLine += " formatter.format(data, 1);";
    graphLine += " formatter.format(data, 2);";
    graphLine += "var chart = new google.visualization." + chartType + "(document.getElementById('" + title + "'));";
    graphLine += " chart.draw(data, options);}";
    graphLine += "</script>";
    graphLine += "<div id=\""+title+"\" style=\"width: 50%; height: 50%;\"></div>";
    return graphLine;
  }

  public string drawGraphThreeAttributeWithFiveFigures(string title, string xAxisName, string firstDataSetName,
   string secondDataSetName, string thirdDataSetName, string[] firstDataSet, string[] secondDataSet, string[] thirdDataSet,
    string[] xAxisData, bool columnChart, bool percentChart)
  {
    string chartType = "LineChart";
    string graphLine = "";
    string format = "#";
    string maxValue = FindValues(firstDataSet, true).ToString();
    string minValue = FindValues(firstDataSet, false).ToString();
    if (columnChart) chartType = "ColumnChart";
    if (percentChart) format = "#,###%";

    graphLine += "<script type=\"text/javascript\">";
    graphLine += "google.load(\"visualization\", \"1\", {packages:[\"corechart\"]});";
    graphLine += "google.setOnLoadCallback(drawChart);";
    graphLine += "function drawChart() {";
    graphLine += "var data = google.visualization.arrayToDataTable([";
    graphLine += "['" + xAxisName + "', '" + firstDataSetName + "','" + secondDataSetName + "','" + thirdDataSetName + "'],";
    graphLine += "['" + xAxisData[0] + "',  " + firstDataSet[0] + ", " + secondDataSet[0] + ", " +thirdDataSet[0]+ "],";
    graphLine += "['" + xAxisData[1] + "',  " + firstDataSet[1] + ", " + secondDataSet[1] + ", " + thirdDataSet[1] + "],";
    graphLine += "['" + xAxisData[2] + "',  " + firstDataSet[2] + ", " + secondDataSet[2] + ", " + thirdDataSet[2] + "],";
    graphLine += "['" + xAxisData[3] + "',  " + firstDataSet[3] + ", " + secondDataSet[3] + ", " + thirdDataSet[3] + "],";
    graphLine += "['" + xAxisData[4] + "',  " + firstDataSet[4] + ", " + secondDataSet[4] + ", " + thirdDataSet[4] + "]]);";
    graphLine += "var options = {";
    graphLine += "title: '" + title + "',";
    graphLine += "titleTextStyle: {color: '#516f91', fontName: 'Open sans', fontSize: 18},";
    graphLine += " vAxis: {minValue:0, maxValue:" + maxValue + ", textStyle: {color: '#666', fontName: 'Open sans', fontSize: 12}, format: '" + format + "'},";
    graphLine += "hAxis: {textStyle: {color: '#516f91', fontName: 'Open sans', fontSize: 12} },";
    graphLine += "tooltip: {textStyle: {color: '#666', fontName: 'Open sans', fontSize: 12}},";
    graphLine += "backgroundColor: {fill: '#fcfcfc', stroke: '#fcfcfc', strokeWidth: '2'},";
    graphLine += "colors: ['#516f91', '#4c937d', '#deb474'],";
    graphLine += " chartArea:{left:60,top:50, width: '80%', height: '55%'}};";
    graphLine += "var formatter = new google.visualization.NumberFormat({pattern: '" + format + "'});";
    graphLine += " formatter.format(data, 1);";
    graphLine += " formatter.format(data, 2);";
    graphLine += " formatter.format(data, 3);";
    graphLine += "var chart = new google.visualization." + chartType + "(document.getElementById('" + title + "'));";
    graphLine += " chart.draw(data, options);}";
    graphLine += "</script>";
    graphLine += "<div id=\"" + title + "\" style=\"width: 50%; height: 50%;\"></div>";
    return graphLine;
  }

  public string drawGraphOneAttributeWithSixFigures(string title, string xAxisName, string firstDataSetName,
     string[] firstDataSet, string[] xAxisData, bool columnChart, bool percentChart)
  {
    string chartType = "LineChart";
    string graphLine = "";
    string format = "#";
    string maxValue = FindValues(firstDataSet, true).ToString();
    string minValue = FindValues(firstDataSet, false).ToString(); 
    if (columnChart) chartType = "ColumnChart";
    if (percentChart) format = "#,###%";

    graphLine += "<script type=\"text/javascript\">";
    graphLine += "google.load(\"visualization\", \"1\", {packages:[\"corechart\"]});";
    graphLine += "google.setOnLoadCallback(drawChart);";
    graphLine += "function drawChart() {";
    graphLine += "var data = google.visualization.arrayToDataTable([";
    graphLine += "['" + xAxisName + "', '" + firstDataSetName + "'],";
    graphLine += "['" + xAxisData[0] + "',  " + firstDataSet[0] + "],";
    graphLine += "['" + xAxisData[1] + "',  " + firstDataSet[1] + "],";
    graphLine += "['" + xAxisData[2] + "',  " + firstDataSet[2] + "],";
    graphLine += "['" + xAxisData[3] + "',  " + firstDataSet[3] + "],";
    graphLine += "['" + xAxisData[4] + "',  " + firstDataSet[4] + "],";
    graphLine += "['" + xAxisData[5] + "',  " + firstDataSet[5] + "]]);";
    graphLine += "var options = {";
    graphLine += "title: '" + title + "',";
    graphLine += "titleTextStyle: {color: '#516f91', fontName: 'Open sans', fontSize: 18},";
    graphLine += " vAxis: {minValue:0, maxValue:" + maxValue + ", textStyle: {color: '#666', fontName: 'Open sans', fontSize: 12}, format: '" + format + "'},";
    graphLine += "hAxis: {textStyle: {color: '#516f91', fontName: 'Open sans', fontSize: 12} },";
    graphLine += "tooltip: {textStyle: {color: '#666', fontName: 'Open sans', fontSize: 12}},";
    graphLine += "backgroundColor: {fill: '#fcfcfc', stroke: 'black', strokeWidth: '2'},";
    graphLine += "series: [{color: '#516f91'}],";
    graphLine += " chartArea:{left:60,top:50, width: '80%', height: '55%'}};";
    graphLine += "var formatter = new google.visualization.NumberFormat({pattern: '" + format + "'});";
    graphLine += " formatter.format(data, 1);";
    graphLine += "var chart = new google.visualization." + chartType + "(document.getElementById('" + title + "'));";
    graphLine += " chart.draw(data, options);}";
    graphLine += "</script>";
    graphLine += "<div id=\""+title+"\" style=\"width: 50%; height: 50%;\"></div>";
    return graphLine;
  }

  public string drawGraphTwoAttributeWithSixFigures(string title, string xAxisName, string firstDataSetName,
    string secondDataSetName, string[] firstDataSet, string[] secondDataSet, string[] xAxisData, bool columnChart,
    bool percentChart)
  {
    string chartType = "LineChart";
    string graphLine = "";
    string format = "#";
    string maxValue = FindValues(firstDataSet, true).ToString();
    string minValue = FindValues(firstDataSet, false).ToString(); 
    if (columnChart) chartType = "ColumnChart";
    if (percentChart) format = "#,###%";

    graphLine += "<script type=\"text/javascript\">";
    graphLine += "google.load(\"visualization\", \"1\", {packages:[\"corechart\"]});";
    graphLine += "google.setOnLoadCallback(drawChart);";
    graphLine += "function drawChart() {";
    graphLine += "var data = google.visualization.arrayToDataTable([";
    graphLine += "['" + xAxisName + "', '" + firstDataSetName + "','" + secondDataSetName + "'],";
    graphLine += "['" + xAxisData[0] + "',  " + firstDataSet[0] + ", " + secondDataSet[0] + "],";
    graphLine += "['" + xAxisData[1] + "',  " + firstDataSet[1] + ", " + secondDataSet[1] + "],";
    graphLine += "['" + xAxisData[2] + "',  " + firstDataSet[2] + ", " + secondDataSet[2] + "],";
    graphLine += "['" + xAxisData[3] + "',  " + firstDataSet[3] + ", " + secondDataSet[3] + "],";
    graphLine += "['" + xAxisData[4] + "',  " + firstDataSet[4] + ", " + secondDataSet[4] + "],";
    graphLine += "['" + xAxisData[5] + "',  " + firstDataSet[5] + ", " + secondDataSet[5] + "]]);";
    graphLine += "var options = {";
    graphLine += "title: '" + title + "',";
    graphLine += "titleTextStyle: {color: '#516f91', fontName: 'Open sans', fontSize: 18},";
    graphLine += " vAxis: {minValue:0, maxValue:" + maxValue + ", textStyle: {color: '#666', fontName: 'Open sans', fontSize: 12}, format: '" + format + "'},";
    graphLine += "hAxis: {textStyle: {color: '#516f91', fontName: 'Open sans', fontSize: 12} },";
    graphLine += "tooltip: {textStyle: {color: '#666', fontName: 'Open sans', fontSize: 12}},";
    graphLine += "backgroundColor: {fill: '#fcfcfc', stroke: 'black', strokeWidth: '2'},";
    graphLine += "series: [{color: '#516f91'}],";
    graphLine += " chartArea:{left:60,top:50, width: '80%', height: '55%'}};";
    graphLine += "var formatter = new google.visualization.NumberFormat({pattern: '" + format + "'});";
    graphLine += " formatter.format(data, 1);";
    graphLine += "var chart = new google.visualization." + chartType + "(document.getElementById('" + title + "'));";
    graphLine += " chart.draw(data, options);}";
    graphLine += "</script>";
    graphLine += "<div id=\""+title+"\" style=\"width: 50%; height: 50%;\"></div>";
    return graphLine;
  }

  private double FindValues(string[] array, bool max)
  {
   int returnType = 1;
   double maxValue = double.Parse(array[0]);
   double minValue = double.Parse(array[0]);
   double[] valueArray = new double[2];

   for (int i = 0; i < array.Length; i++)
   {
     if (maxValue < double.Parse(array[i])) 
       maxValue = double.Parse(array[i]);
     if(minValue > double.Parse(array[i]))
       minValue = double.Parse(array[i]);
   }
   if (max)
     valueArray[0] = Math.Round(maxValue, 1);
   else
     valueArray[1] = Math.Round(minValue, 1);
   if (max) returnType = 0;
   return valueArray[returnType];
  }

  private void UseLibary()
  {
    libaryAccesString = "<script type=\"text/javascript\" src=\"https://www.google.com/jsapi\"></script>";
  }
}